package com.kilomob.grady.spring;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 * 
 * reference解析类
 * 
 *   
 * @author zhouzhixiang  
 * @date 2016年12月31日
 * @since 1.0
 */
public class GradyBeanReferenceParser implements BeanDefinitionParser {
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        
        String interfaceName = element.getAttribute("interface");
        String protocolType = element.getAttribute("protocol");
        String id = element.getAttribute("id");
        String clientId = element.getAttribute("clientId");
        String oneway = element.getAttribute("oneway");
        String async = element.getAttribute("async");

        RootBeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setBeanClass(ReferenceBean.class);
        beanDefinition.setLazyInit(false);

        beanDefinition.getPropertyValues().addPropertyValue("interface", interfaceName);
        beanDefinition.getPropertyValues().addPropertyValue("protocol", protocolType);
        beanDefinition.getPropertyValues().addPropertyValue("id", id);
        beanDefinition.getPropertyValues().addPropertyValue("clientId", clientId);
        beanDefinition.getPropertyValues().addPropertyValue("oneway", Boolean.valueOf(oneway));
        beanDefinition.getPropertyValues().addPropertyValue("async", Boolean.valueOf(async));

        parserContext.getRegistry().registerBeanDefinition(id,beanDefinition);
        return beanDefinition;
    }
}
